package com.canbot.u05.activity.customvoice

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.view.View
import com.canbot.u05.R
import com.canbot.u05.activity.BaseActivityNoTitle
import com.canbot.u05.activity.customvoice.adapter.CustomDialogActionAdapter
import com.canbot.u05.activity.customvoice.bean.CustomActionData
import com.canbot.u05.activity.getHandler
import com.canbot.u05.activity.overridePendingTransitionExt
import com.canbot.u05.socket.MsgSendUtils
import com.canbot.u05.socket.MsgType
import com.canbot.u05.utils.PathConst
import kotlinx.android.synthetic.main.activity_custom_dialog_add_action.*
import java.util.ArrayList

/**
 * CustomDialogAddActionActivity
 *
 * @author  Liu.Heng
 * @date  2019/5/29
 */
class CustomDialogAddActionActivity : BaseActivityNoTitle() {

    private var checkId = 1

    private lateinit var mCustomActionAdapter: CustomDialogActionAdapter

    private lateinit var mCustomActionData: MutableList<CustomActionData>

    private lateinit var mHead: Array<String>

    private lateinit var mArm: Array<String>

    private lateinit var mBoot: Array<String>

    private lateinit var mHeadId: Array<String>

    private lateinit var mArmId: Array<String>

    private lateinit var mBootId: Array<String>

    private lateinit var actionId: String

    private val mHandler = getHandler { msg ->
        mCustomActionData.clear()
        mCustomActionAdapter.setChecked(-1)
        when (msg.what) {
            1 -> getHeadData()
            2 -> getHandData()
            3 -> getLegData()
            else -> {
            }
        }

        mCustomActionData.withIndex().find {
            actionId == it.value.actionID
        }?.let {
            mCustomActionAdapter.setChecked(it.index)
        }

        mCustomActionAdapter.notify(mCustomActionData)
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_custom_dialog_add_action)
        val intent = intent
        actionId = intent.getStringExtra("actionId")
        if (actionId.isNotEmpty()) {
            my_next.visibility = View.VISIBLE
        } else {
            my_next.visibility = View.GONE
        }
        initView()
    }

    override fun onResume() {
        super.onResume()
        MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND, PathConst.CUSTOM_DIALOG_PAYH + "u05_dzdz_001.wav")
        MsgSendUtils.sendStringMsg(MsgType.EYE_MOTION, "14")
    }

    fun initView() {
        relativeLayout1.setOnClickListener(::onClick)
        relativeLayout2.setOnClickListener(::onClick)
        relativeLayout3.setOnClickListener(::onClick)
        my_back.setOnClickListener(::onClick)
        my_next.setOnClickListener(::onClick)

        mHead = resources.getStringArray(R.array.head_text)
        mArm = resources.getStringArray(R.array.arm_text)
        mBoot = resources.getStringArray(R.array.boot_text)
        mHeadId = resources.getStringArray(R.array.head_id)
        mArmId = resources.getStringArray(R.array.arm_id)
        mBootId = resources.getStringArray(R.array.boot_id)
        action_recyclerView.layoutManager = GridLayoutManager(this@CustomDialogAddActionActivity, 5) //网格布局，5
        mCustomActionData = ArrayList()


        mCustomActionAdapter = CustomDialogActionAdapter(this, mCustomActionData, object : CustomDialogActionAdapter.OnItemClickListener {

            override fun onItemClick(view: View) {
                my_next.visibility = View.VISIBLE
                val i = view.tag as Int
                mCustomActionAdapter.setChecked(i)
                mCustomActionAdapter.notifyDataSetChanged()
                when (checkId) {
                    1 -> actionId = mCustomActionData[i].actionID
                    2 -> actionId = mCustomActionData[i].actionID
                    3 -> actionId = mCustomActionData[i].actionID
                    else -> {
                    }
                }
            }
        })

        action_recyclerView.adapter = mCustomActionAdapter

        if (actionId.isEmpty()) {
            head_imageView.visibility = View.VISIBLE
            mHandler.sendEmptyMessage(checkId)
        } else {

            mHeadId.find {
                actionId == it
            }?.let {
                checkId = 1
                head_imageView.visibility = View.VISIBLE
            }

            mArmId.find {
                actionId == it
            }?.let {
                checkId = 2
                hand_imageView.visibility = View.VISIBLE
            }

            mBootId.find {
                actionId == it
            }?.let {
                checkId = 3
                leg_imageView.visibility = View.VISIBLE
            }

            mHandler.sendEmptyMessage(checkId)
        }
    }

    /**获取头部动作*/
    private fun getHeadData() {
        mHeadId.withIndex().forEach {
            mCustomActionData.add(CustomActionData(it.value, R.drawable.custom_action_pic, mHead[it.index]))
        }
    }

    /**回去手部动作*/
    private fun getHandData() {
        mArmId.withIndex().forEach {
            mCustomActionData.add(CustomActionData(it.value, R.drawable.custom_action_pic, mArm[it.index]))
        }
    }

    /**获取腿部动作*/
    private fun getLegData() {
        mBootId.withIndex().forEach {
            mCustomActionData.add(CustomActionData(it.value, R.drawable.custom_action_pic, mBoot[it.index]))
        }
    }

    private fun resetImage() {
        head_imageView.visibility = View.GONE
        hand_imageView.visibility = View.GONE
        leg_imageView.visibility = View.GONE
    }

    private fun onClick(view: View) {

        when (view.id) {
            R.id.relativeLayout1 -> {
                resetImage()
                checkId = 1
                head_imageView.visibility = View.VISIBLE
            }
            R.id.relativeLayout2 -> {
                resetImage()
                checkId = 2
                hand_imageView.visibility = View.VISIBLE
            }
            R.id.relativeLayout3 -> {
                resetImage()
                checkId = 3
                leg_imageView.visibility = View.VISIBLE
            }
            R.id.my_back -> {
                setResult(Activity.RESULT_CANCELED)
                finish()
                overridePendingTransitionExt()
            }
            R.id.my_next -> {
                val intent = Intent()
                intent.putExtra("actionId", actionId)
                setResult(Activity.RESULT_OK, intent)
                finish()
                overridePendingTransitionExt()
            }
            else -> {
            }
        }
        mHandler.sendEmptyMessage(checkId)
    }


}
